package org.wzl.todolisthelper.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.wzl.todolisthelper.convert.NormalTaskConvert;
import org.wzl.todolisthelper.domain.NormalTask;
import org.wzl.todolisthelper.domain.TaskEvent;
import org.wzl.todolisthelper.dto.AddNormalTaskDTO;
import org.wzl.todolisthelper.enumerate.AddTaskEventDTO;
import org.wzl.todolisthelper.enumerate.DelFlagEnum;
import org.wzl.todolisthelper.enumerate.NormalTaskStatusEnum;
import org.wzl.todolisthelper.enumerate.TaskEventEnum;
import org.wzl.todolisthelper.exception.BizException;
import org.wzl.todolisthelper.service.NormalTaskService;
import org.wzl.todolisthelper.mapper.NormalTaskMapper;
import org.springframework.stereotype.Service;
import org.wzl.todolisthelper.service.TaskCommentService;
import org.wzl.todolisthelper.service.TaskEventService;
import org.wzl.todolisthelper.utils.UserUtil;
import org.wzl.todolisthelper.vo.FrontStepsComponent;
import org.wzl.todolisthelper.vo.IndexTaskCardVO;
import org.wzl.todolisthelper.vo.NormalTaskDetailVO;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
* @author weizhilong
* @description 针对表【t_normal_task】的数据库操作Service实现
* @createDate 2025-01-31 23:52:59
*/
@Service
public class NormalTaskServiceImpl extends ServiceImpl<NormalTaskMapper, NormalTask>
    implements NormalTaskService {

    @Resource
    private NormalTaskConvert normalTaskConvert;

    @Resource
    private TaskEventService taskEventService;

    @Resource
    private TaskCommentService taskCommentService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addTask(AddNormalTaskDTO addNormalTaskDTO) {
        LocalDateTime taskStartDate = addNormalTaskDTO.getTaskStartDate();
        LocalDateTime taskEndDate = addNormalTaskDTO.getTaskEndDate();
        if (taskStartDate.isAfter(taskEndDate)) {
            throw new BizException("开始时间较晚");
        }

        NormalTask task = normalTaskConvert.toDO(addNormalTaskDTO);

        boolean save = this.save(task);
        if (!save) {
            throw new BizException("添加任务失败");
        }

        AddTaskEventDTO addTaskEventDTO = new AddTaskEventDTO();
        addTaskEventDTO.setEventType(TaskEventEnum.INSERT);
        addTaskEventDTO.setEventName("添加任务");
        addTaskEventDTO.setTaskId(task.getId());
        taskEventService.addTaskEvent(addTaskEventDTO);
    }

    @Override
    public List<IndexTaskCardVO> getAllCard() {
        List<NormalTask> allDO = list(Wrappers.<NormalTask>lambdaQuery()
                .eq(NormalTask::getCreateUserId, UserUtil.getCurrentUserId())
                .eq(NormalTask::getDelFlag, DelFlagEnum.NOT_DEL.getCode())
                .eq(NormalTask::getTaskStatus, NormalTaskStatusEnum.DOING.getCode())
                .orderBy(true, false, NormalTask::getCreateDate));
        return allDO.stream().map(normalTaskConvert::toVO).toList();
    }

    @Override
    public NormalTaskDetailVO getDetailById(String id) {
        NormalTaskDetailVO detailVO = normalTaskConvert.toDetailVO(getById(id));

        List<TaskEvent> taskEventList = taskEventService.list(Wrappers.<TaskEvent>lambdaQuery()
                .eq(TaskEvent::getTaskId, id)
                .orderBy(true, true, TaskEvent::getCreateDate));

        List<FrontStepsComponent.Step> list = taskEventList
                .stream().map(taskEvent -> {
                    FrontStepsComponent.Step step = new FrontStepsComponent.Step();
                    step.setTitle(taskEvent.getEventName());
                    if (ObjectUtils.isEmpty(taskEvent.getEventDesc())) {
                        step.setContent(taskEvent.getCreateDate().format(DateTimeFormatter.ISO_DATE));
                    } else {
                        step.setContent(taskEvent.getEventDesc());
                    }
                    return step;
                }).toList();
        FrontStepsComponent frontStepsComponent = new FrontStepsComponent();
        frontStepsComponent.setCurrent(list.size() - 1);
        frontStepsComponent.setSteps(list);

        detailVO.setTaskEvent(frontStepsComponent);
        detailVO.setComments(taskCommentService.getTaskComments(id));
        return detailVO;
    }

    @Override
    public void completeTask(String taskId) {
        NormalTask normalTask = new NormalTask();
        normalTask.setId(taskId);
        normalTask.setTaskStatus(NormalTaskStatusEnum.FINISH.getCode());

        updateById(normalTask);

        AddTaskEventDTO addTaskEventDTO = new AddTaskEventDTO();
        addTaskEventDTO.setEventType(TaskEventEnum.COMPLETED);
        addTaskEventDTO.setEventName("完成任务");
        addTaskEventDTO.setEventDesc("");
        addTaskEventDTO.setTaskId(taskId);
        taskEventService.addTaskEvent(addTaskEventDTO);
    }

    @Override
    public void deleteTask(String taskId) {
        NormalTask normalTask = new NormalTask();
        normalTask.setId(taskId);
        normalTask.setDelFlag(DelFlagEnum.DEL.getCode());

        updateById(normalTask);

        AddTaskEventDTO addTaskEventDTO = new AddTaskEventDTO();
        addTaskEventDTO.setEventType(TaskEventEnum.DELETE);
        addTaskEventDTO.setEventName("删除任务");
        addTaskEventDTO.setEventDesc("");
        addTaskEventDTO.setTaskId(taskId);
        taskEventService.addTaskEvent(addTaskEventDTO);
    }

    private List<NormalTask> getAllDO() {
        return list(this.getAvailableTaskWrappers());
    }

    private LambdaQueryWrapper<NormalTask> getAvailableTaskWrappers() {
        return Wrappers.<NormalTask>lambdaQuery()
                .eq(NormalTask::getCreateUserId, UserUtil.getCurrentUserId())
                .in(NormalTask::getTaskStatus, NormalTaskStatusEnum.getAvailableStatus());
    }

}




